home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / allison / bitstr.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-13  |  4.8 KB  |  224 lines

  1. LISTING 4 - Bitstring Class Definition and Inline Functions
  2. // bitstr.hpp:    The C++ bitstring class
  3.  
  4. #if !defined(BITSTR_H)
  5. #define BITSTR_H
  6.  
  7. #include <stddef.h>
  8. #include <limits.h>
  9. #include <assert.h>
  10. #include "string.hpp"
  11.  
  12. class istream;
  13. class ostream;
  14.  
  15. class bitstring
  16. {
  17. public:
  18.     bitstring();
  19.     bitstring(unsigned long, size_t);
  20.     bitstring(const string&);
  21.     bitstring(const bitstring&);
  22.     ~bitstring();
  23.  
  24.     // Conversions
  25.     string to_string() const;
  26.  
  27.     // Assignment
  28.     bitstring& operator=(const bitstring&);
  29.  
  30.     // Equality
  31.     int operator==(const bitstring&) const;
  32.     int operator!=(const bitstring&) const;
  33.  
  34.     // Basic bit operations
  35.     bitstring& set(size_t, int = 1);
  36.     bitstring& set();
  37.     bitstring& reset(size_t);
  38.     bitstring& reset();
  39.     bitstring& toggle(size_t);
  40.     bitstring& toggle();
  41.     int test(size_t) const;
  42.     int any() const;
  43.     int none() const;
  44.     bitstring operator~() const;
  45.     size_t count() const;
  46.  
  47.     // Bitwise operators
  48.     bitstring& operator&=(const bitstring&);
  49.     bitstring& operator|=(const bitstring&);
  50.     bitstring& operator^=(const bitstring&);
  51.     bitstring& operator>>=(size_t);
  52.     bitstring& operator<<=(size_t);
  53.     bitstring operator>>(size_t) const;
  54.     bitstring operator<<(size_t) const;
  55.  
  56.     // String operations
  57.     bitstring& operator+=(const bitstring&);
  58.     bitstring& insert(size_t, const bitstring&);
  59.     bitstring& remove(size_t, size_t);
  60.     bitstring& replace(size_t, size_t, const bitstring&);
  61.     size_t find(int, size_t = 0) const;
  62.     size_t rfind(int, size_t = NPOS) const;
  63.     bitstring substr(size_t, size_t) const;
  64.     size_t length() const;
  65.     size_t length(size_t, int = 0);
  66.     size_t trim();
  67.  
  68. private:
  69.     typedef unsigned int Block;
  70.     Block *bits_;
  71.     size_t nbits_;
  72.     size_t nblks_;
  73.     Block clean_mask_;
  74.  
  75.     enum {BLKSIZ = CHAR_BIT * sizeof(Block)};
  76.  
  77.     static Block word(size_t b)
  78.       {return b / BLKSIZ;}
  79.     static Block offset(size_t b)
  80.       {return BLKSIZ - b%BLKSIZ - 1;}
  81.     static Block mask1(size_t b)
  82.       {return Block(1) << offset(b);}
  83.     static Block mask0(size_t b)
  84.       {return ~(Block(1) << offset(b));}
  85.     static size_t nblks(size_t nb)
  86.       {return (nb+BLKSIZ-1) / BLKSIZ;}
  87.  
  88.     void make_clean_mask();
  89.     void cleanup();
  90.     void set_(size_t);
  91.     int set_(size_t, int);
  92.     void reset_(size_t);
  93.     int test_(size_t) const;
  94.     void from_string(const string&);
  95.     void init(size_t);
  96.     void equalize(bitstring&);
  97.  
  98.     friend istream& operator>>(istream&, bitstring&);
  99. };
  100.  
  101. // Global Functions:
  102. ostream&  operator<<(ostream&, const bitstring&);
  103. istream&  operator>>(istream&, bitstring&);
  104. bitstring operator& (const bitstring&, const bitstring&);
  105. bitstring operator| (const bitstring&, const bitstring&);
  106. bitstring operator^ (const bitstring&, const bitstring&);
  107. bitstring operator+ (const bitstring&, const bitstring&);
  108.  
  109. // Inline publics:
  110. inline bitstring::bitstring()
  111. {
  112.     init(0);
  113. }
  114.  
  115. inline bitstring::~bitstring()
  116. {
  117.     delete [] bits_;
  118. }
  119.  
  120. inline bitstring& bitstring::toggle(size_t pos)
  121. {
  122.     assert(pos < nbits_);
  123.     bits_[word(pos)] ^= mask1(pos);
  124.     return *this;
  125. }
  126.  
  127. inline int bitstring::test(size_t pos) const
  128. {
  129.     assert(pos < nbits_);
  130.     return test_(pos);
  131. }
  132.  
  133. inline bitstring bitstring::operator~() const
  134. {
  135.     bitstring b(*this);
  136.     b.toggle();
  137.     return b;
  138. }
  139.  
  140. inline int bitstring::operator!=(const bitstring& b) const
  141. {
  142.     return !operator==(b);
  143. }
  144.  
  145. inline int bitstring::none() const
  146. {
  147.     return !any();
  148. }
  149.  
  150. inline size_t bitstring::length() const
  151. {
  152.     return nbits_;
  153. }
  154.  
  155. inline bitstring
  156. operator&(const bitstring& x, const bitstring& y)
  157. {
  158.     bitstring b(x);
  159.     return b &= y;
  160. }
  161.  
  162. inline bitstring
  163. operator|(const bitstring& x, const bitstring& y)
  164. {
  165.     bitstring b(x);
  166.     return b |= y;
  167. }
  168.  
  169. inline bitstring
  170. operator^(const bitstring& x, const bitstring& y)
  171. {
  172.     bitstring b(y);
  173.     return b ^= x;
  174. }
  175.  
  176. inline bitstring bitstring::operator<<(size_t n) const
  177. {
  178.     bitstring r(*this);
  179.     return r <<= n;
  180. }
  181.  
  182. inline bitstring bitstring::operator>>(size_t n) const
  183. {
  184.     bitstring r(*this);
  185.     return r >>= n;
  186. }
  187.  
  188. inline bitstring
  189. operator+(const bitstring& b1, const bitstring& b2)
  190. {
  191.     bitstring b(b1);
  192.     return b.operator+=(b2);
  193. }
  194.  
  195. // Inline privates:
  196. inline void bitstring::make_clean_mask()
  197. {
  198.     clean_mask_ = ~Block(0) << (nblks_ * BLKSIZ - nbits_);
  199. }
  200.  
  201. inline void bitstring::cleanup()
  202. {
  203.     // Make sure unused bits don't get set
  204.     if (nbits_ % BLKSIZ)
  205.         bits_[nblks_ - 1] &= clean_mask_;
  206. }
  207.  
  208. inline void bitstring::set_(size_t b)
  209. {
  210.     bits_[word(b)] |= mask1(b);
  211. }
  212.  
  213. inline void bitstring::reset_(size_t b)
  214. {
  215.     bits_[word(b)] &= mask0(b);
  216. }
  217.  
  218. inline int bitstring::test_(size_t b) const
  219. {
  220.     return !!(bits_[word(b)] & mask1(b));
  221. }
  222.  
  223. #endif
  224.